/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOS_STL_PROTOCOL_PACKAGE_PKG_RET_LIST_BASE_REQ_H__
#define __XOS_STL_PROTOCOL_PACKAGE_PKG_RET_LIST_BASE_REQ_H__

#include <string>
#include "../../import/head.h"
#include "../../tools/head.h"
#include "../db/db_get.h"
#include "pkg_public_functions.h"
#include "pkg_base_req.h"

namespace xos_protocol
{

    template< class CHILD, class T, class PACKER, class REQUEST, class SESSION, class PACKAGE >
    class pkg_base_ret_list_req : public pkg_base_req<
        pkg_base_ret_list_req< CHILD, T, PACKER, REQUEST, SESSION, PACKAGE >,
        REQUEST,
        SESSION,
        PACKAGE >
    {
    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        typedef pkg_base_ret_list_req< CHILD, T, PACKER, REQUEST, SESSION, PACKAGE > THIS_CHILD;
        typedef pkg_base_req<
            THIS_CHILD,
            REQUEST,
            SESSION,
            PACKAGE > BASE;

    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 

        pkg_base_ret_list_req( xos_box::i_serial * pSerial, xos_container::i_container * pContainer, int nProtocol ) :
            BASE( pSerial, pContainer, nProtocol )
        {
            page_size = 0;
            page_num = 0;
            cur_page = 0;
        }

        ~pkg_base_ret_list_req()
        {
        }

    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        uint16_t page_size;
        uint16_t page_num;
        uint16_t cur_page;

    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        int helper_dis_compose( REQUEST * pRequest, SESSION * pSession, xos_box::i_buf * pBuf )
        {
            int ret = 0;

            xos_protocol::db_get< T, PACKER > obj( BASE::serial_ptr, pBuf );
            T * pT = (T*)pRequest->m_pDatas[0];
            int nTotalNum = 0;
            int nRet = 0;

            if( 0 == ret )
            {
                typename T::LIST * pList = pT ? ( typename T::LIST* )&pT->m_list : 0;
                ret = obj.res_dis_compose( nRet, pList );
                if( ( 0 == ret ) && ( 0 != nRet ) )
                {
                    pRequest->m_strs[0] = obj.m_err;
                }
            }

            if( 0 == nRet )
            {
                pRequest->m_datas[0] = obj.page_size;
                pRequest->m_datas[1] = obj.page_num;
                pRequest->m_datas[2] = obj.cur_page;
                pRequest->m_nRecvNum += obj.this_num;
                nTotalNum = obj.total_num;
            }

            if( ( 0 != nRet ) || ( nTotalNum == pRequest->m_nRecvNum ) )
            {
                pSession->m_request_list.remove( pRequest );
                pRequest->m_bSuccess = 0 == nRet;
                REQUEST::response( pRequest );
                pRequest = 0;
            }

            return ret;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        int helper_compose( REQUEST * pRequest, xos_box::i_buf * pBuf )
        {
            CHILD * pThis = static_cast<CHILD*>( this );
            return pThis->helper_compose( pRequest, pBuf );
        }

    };

} // xos_protocol

#endif // __XOS_STL_PROTOCOL_PACKAGE_PKG_RET_LIST_BASE_REQ_H__
